import {PageContainer} from "@ant-design/pro-components";
import CustomerChannelTable from "@/pages/CustomerChannel/components/CustomerChannelTable";
import {CustomerChannelVO, saveCustomerChannel, updateCustomerChannel} from "@/services/CustomerChannelService";
import CustomerChannelUpdateModal from "@/pages/CustomerChannel/components/CustomerChannelUpdateModal";
import {useState} from "react";
import {message} from "antd";

const CustomerChannel = () => {
  // 编辑操作
  const [updateModalState, setUpdateModalState] = useState(false)
  const [currentCustomerChannel, setCurrentCustomerChannel] = useState<CustomerChannelVO | undefined>()
  const updateEvent = async (record: CustomerChannelVO) => {
    // 打开模态框
    setCurrentCustomerChannel(record)
    setUpdateModalState(true)
  }

  const saveEvent = async () => {
    // 打开模态框
    setCurrentCustomerChannel(undefined)
    setUpdateModalState(true)
  }
  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let response
    if (currentCustomerChannel) {
      response = await updateCustomerChannel(formData);
    } else {
      response = await saveCustomerChannel(formData)
    }
    if (response) {
      message.success("操作成功")
      return true
    }
    return false;
  }

  return <PageContainer title="客资渠道">
    {/*客资渠道表格 开始*/}
    <CustomerChannelTable onSave={saveEvent} onUpdate={updateEvent}/>
    {/*客资渠道表格 结束*/}

    {/*修改模态框 开始*/}
    <CustomerChannelUpdateModal
      customerChannel={currentCustomerChannel}
      open={updateModalState}
      onOpenChange={setUpdateModalState}
      onFinish={onUpdateFinish}/>
    {/*修改模态框 结束*/}
  </PageContainer>
}

export default CustomerChannel
